def bidirectionalSelectionSort(arr):
    n = len(arr)
    left = 0
    right = n - 1
    count = 0
    while left < right:
        minIndex = left
        maxIndex = left
        for i in range(left, right + 1):
            count += 1
            if arr[i] < arr[minIndex]:
                minIndex = i
            if arr[i] > arr[maxIndex]:
                maxIndex = i
        # 交换最小值到left位置
        if minIndex!= left:
            arr[left], arr[minIndex] = arr[minIndex], arr[left]
            # 如果最大值在left位置，更新maxIndex
            if maxIndex == left:
                maxIndex = minIndex
        # 交换最大值到right位置
        if maxIndex!= right:
            arr[right], arr[maxIndex] = arr[maxIndex], arr[right]
        left += 1
        right -= 1
    print(f"函数运行次数: {count}")
    return arr

arr = [5, 3, 6, 2, 10, 14, 19, 15, 7, 8, 9, 1, 4, 11, 13, 12, 16, 17, 18]
sorted_arr = bidirectionalSelectionSort(arr)
print(sorted_arr)